package com.xiaoge.system.controller;

import com.xiaoge.auth.annotation.ControllerEndpoint;
import com.xiaoge.response.Result;
import com.xiaoge.system.entity.File;
import com.xiaoge.system.service.AliOssService;
import com.xiaoge.system.service.impl.AliOssServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

@Api
@RestController
//允许跨域
@CrossOrigin
@Data
public class AliOssController {

    final AliOssService aliOssService;

    public AliOssController(AliOssService aliOssService) {
        this.aliOssService = aliOssService;
    }


    /**
     * 下载文件
     *
     * @param file
     * @param response
     * @throws InterruptedException
     * @throws IOException
     */
    @ControllerEndpoint(exceptionMessage = "下载文件失败", operation = "下载文件")
    @PostMapping(value = "/fileDownload")
    public void fileDownload(String file, HttpServletResponse response) throws InterruptedException, IOException {
        response.setHeader("content-type", "application/octet-stream");
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(file, "UTF-8"));
        aliOssService.downLoadFile(response, file);
    }


    /**
     * 上传文件
     */
    @ControllerEndpoint(exceptionMessage = "上传文件到oss失败", operation = "上传文件oss")
    @ApiOperation("oss上传文件")
    @PostMapping("/uploadFile")
    // @RequiresPermissions("basedata:oesmembers:upload")
    public Result uploadFile(MultipartFile file) throws IOException {
        File file1 = null;
        try {
            file1 = aliOssService.upload(file);
        } catch (Exception e) {
            return Result.error().message("上传失败");
        }
        return Result.ok().data("file", file1);
    }

    /**
     * 列举文件
     */
    @ApiOperation("oss列举文件")
    @PostMapping("/listFile")
    public Result listFile() {
        List<File> files;
        files = aliOssService.listFile();
        return Result.ok().data("files", files);
    }

    /**
     * 删除文件
     *
     * @param file
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除oss文件失败", operation = "删除oss文件")
    @ApiOperation("oss删除文件")
    @PostMapping("/deleteFile")
    public Result deleteFile(String file) {
        //切割路径 "http://xinguan-my-parent.oss-cn-chengdu.aliyuncs.com/2021/02/06/366e26d53a9c4088941ca08c6edf8a85.png"
        if (file != null) {
            String[] split = file.split(".com/");
            aliOssService.deleteFile(split[1]);
            return Result.ok().message("删除aliOss文件成功");
        } else {
            return Result.error().message("删除aliOss文件失败");
        }
    }

    /**
     * 删除文件
     *
     * @param files
     * @return
     */
    @ApiOperation("oss批量删除文件")
    @PostMapping("/deleteFiles")
    public Result deleteFiles(String files) {
        String[] urlList = files.split(",");
        //切割路径 "http://xinguan-my-parent.oss-cn-chengdu.aliyuncs.com/2021/02/06/366e26d53a9c4088941ca08c6edf8a85.png"
        if (urlList.length > 0) {
            for (String s : urlList) {
                String[] split = s.split(".com/");
                aliOssService.deleteFile(split[1]);
            }
        }
        return Result.ok().message("删除aliOss文件成功");
    }
}
